<?php

namespace app\modules\Admin\service;

use app\modules\Admin\models\agentgoodslogModel;

class Agentgoodslog
{
    private static array $event = [
        'add',
        'edit',
        'del'
    ];

    private static array $oper_type = [
        'admin',
        'system',
        'user',
    ];

    /**
     * @param array $logInfo ['goods_id'=> 0, 'event', ....]
     * @return bool
     */
    public static function addLog(array $logInfo):bool
    {
        $newData = [];
        if(empty($logInfo['goods_id'])){
            return false;
        }
        if(empty($logInfo['event']) || !in_array($logInfo['event'], self::$event)){
            return false;
        }
        if(empty($logInfo['oper_type']) || !in_array($logInfo['oper_type'], self::$oper_type)){
            return false;
        }
        $current_set = \agentgoodsModel::query()->where('goods_id', $logInfo['goods_id'])->first();
        if(!$current_set){
            return false;
        }
        $newLogInfo = $current_set->toArray();
        $newLogInfo['event'] = $logInfo['event'];
        $newLogInfo['oper_type'] = $logInfo['oper_type'];
        $newLogInfo['oper_id'] = $logInfo['oper_id'];
        $newLogInfo['created_at'] = timestamp();
        agentgoodslogModel::query()->insert($newLogInfo);
        return true;
    }
}